<?php
namespace App\Seeder;
use App\Http\Common\MyUtils;
use App\Models\Permission;
use App\Models\WyPropertys;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;

/**
 * 新增权限
 * Class AddPropertySeeder
 */
class AddPermissionSeeder extends BaseSeeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $type = $this->data['type'];
        $wy_table_id = $this->data['wy_table_id'];
        $this->addRole($wy_table_id,$type);
    }

    /**
     * 新增导航
     * @param $groupId
     * @param $type
     */
    public function addRole($wy_table_id,$type){
        $projectId = MyUtils::getSessionConfig('PROJECT_ID');
        switch ($type){
            case 1:// 小区
                break;
            case 2:// 物业
                $property = WyPropertys::where('id',$wy_table_id)->get()->first();
                $groupId = $property->group_id;
                $roleId =$this->addRoles($groupId,$projectId,"小区管理员","小区的所有功能权限",2);
                $this->addPermissions($roleId,$projectId,'小区');
                $roleId =$this->addRoles($groupId,$projectId,"物业管理员","物业的所有功能权限",1);
                $this->addPermissions($roleId,$projectId,'物业');
                $roleId =$this->addRoles($groupId,$projectId,"超级管理员","物业和小区的所有功能权限",1);
                $this->addPermissions($roleId,$projectId,'物业');
                $this->addPermissions($roleId,$projectId,'小区');
                break;
            case 3:// 运营商
                break;
        }
    }


    /**
     * 新增角色
     * @param $groupId
     * @param $projectId
     * @param $name
     * @param $description
     * @param $groupType 1物业 2小区
     * @return int
     */
    public function addRoles($groupId,$projectId,$name,$description,$groupType){
        $roleId = DB::table('roles')->insertGetId([
            'name' => $name,
            'description' => $description,
            'enable' => 1,
            'group_id' => $groupId,
            'group_type' => $groupType,
            'project_id' => $projectId,
        ]);
        return $roleId;
    }

    /**
     * 新增权限
     * @param $roleId
     * @param $projectId
     * @param $roleName 权限名字 超级管理员 运营商 物业 小区 商家
     * @param int $level 0表示是后端自动生成的，1表示是前端传递来的
     */
    public function addPermissions($roleId,$projectId,$roleName,$level=1){
        $permission = Permission::where('name',$roleName)->get()->first();
        if ($permission){
            DB::table('role_permission')->insert([
                'role_id' => $roleId,
                'permission_id' => $permission->id,
                'project_id' => $projectId,
                'level' => $level,
            ]);
            $permissions = MyUtils::getAllChildrenIds2($permission->id,'permissions');
            $permissions = array_unique(Arr::pluck($permissions, 'id'));
            foreach ($permissions as $k=>$v){
                $permissionId = DB::table('role_permission')->insert([
                    'role_id' => $roleId,
                    'permission_id' => $v,
                    'project_id' => $projectId,
                    'level' => $level,
                ]);
            }
        }
    }
}
